package ekahau.ue.registration.server;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.util.Vector;

import javax.swing.undo.UndoableEditSupport;

import ekahau.positioning.services.providor.server.EkahauClientListener;
import ekahau.ue.registration.IAreaUpdate;
import ekahau.ue.registration.IPreferenceManager;
import ekahau.ue.registration.RegisteredClientResponse;
import ekahau.ue.registration.RegistrationClientRequest;


public class UERegistrationServer extends Thread implements IAreaUpdate{
    
	private Vector<UERegistrationListener> registeredClientList = new Vector<UERegistrationListener>();
	/**
     * The port that the server listens on.
     */
    private static final int PORT = 9004;
    private IPreferenceManager prefManager;
    
    public UERegistrationServer(IPreferenceManager pManager){
    	prefManager = pManager;
    	prefManager.addAreaChangeListener(this);
    }
    
	public void removeClient(UERegistrationListener pl){
		
		registeredClientList.remove(pl);
		
	}
	public void addClient(UERegistrationListener listener){
		
		 registeredClientList.add(listener);
	}
	
	synchronized public void processRequest(RegistrationClientRequest rCReq, 
		 													ObjectOutputStream oStream){
		
		RegisteredClientResponse response = prefManager.processPreferenceRequest(rCReq);
		ObjectOutputStream objStream;
		try {
			//objStream = new ObjectOutputStream(oStream);
			oStream.writeObject(response);
			oStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
     * The appplication main method, which just listens on a port and
     * spawns handler threads.
     */
    public void run(){
        
    	System.out.println("UE Registration server is running.");
    	//UERegistrationServer servicesProvidor = new UERegistrationServer();
        ServerSocket listener = null;
		
        try {
        	listener = new ServerSocket(PORT);
        
            while (true) {
                new UERegistrationListener(listener.accept(),this).start();
            }
        }catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
            try {
            	if(listener != null)
            		listener.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }
	@Override
	public void notifyAreaChanged(final String macID,final String zName, final String services[]) {
		
		Runnable sender = new Runnable() {
			
			@Override
			public void run() {
				UERegistrationListener listener = getServicesThread(macID);
				if(listener == null)
					return ;
				RegisteredClientResponse response = new RegisteredClientResponse(services, zName);
				try {
					listener.getOutputSteam().writeObject(response);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		};
		
		sender.run();
	}
	
	public UERegistrationListener getServicesThread(String id){
		
		 for(int i = 0 ; i < registeredClientList.size() ; i++){
			 UERegistrationListener listener = registeredClientList.get(i);
			 if(listener.getMacID().compareTo(id) == 0 )
				 return listener;
		 }
		 
		 return null;
	}
}
